fn main() {
    //1.0
    /*     let mut s = String::from("hello world");
    let c = 'e';
    let word = first_word(&s, &c); // word will get the value 5
    println!("{}", word);
    s.clear(); // this empties the String, making it equal to "" */

    //2.0
    /*     let s = String::from("hello,world");
    let hello = &s[0..5];

    let world = &s[6..11];
    let _space = &s[5..5];
    println!("{}", hello);
    println!("{}", world);
    println!("{}", _space); */

    //3.0
    /*     let s = String::from("hello,world");
    let s1 = &s[0..2];
    println!("{}", s1);
    let s1 = &s[..2];
    println!("{}", s1); */

    //4.0
    let s = String::from("hello,world");
    let target_char = String::from("r");
    let first_word = first_word(&s, &target_char);

    println!("{}", first_word);
}

//根据字节编码返回指定单词
/* fn first_word(s: &String, c: &char) -> usize {
    let bytes = s.as_bytes();
    let taget_char: u8 = char::from(*c).to_string().bytes().next().unwrap();
    for (i, &item) in bytes.iter().enumerate() {
        let _char_value = char::from(item); // 将 u8 类型的值转换为 char 类型
        println!("{}", _char_value);
        if item == taget_char {
            println!("{}", item);
            return i;
        }
    }

    s.len()
} */

//根据切片返回指定切片

/* fn first_workd(s: &String, c: &String) -> &str {
    let bytes = s.as_bytes();
    let target_char = c.bytes().next().unwrap();
    for (i, &item) in bytes.iter().enumerate() {
        if item == target_char {
            return &s[0..i];
        }
    }
    &s[..]
} */
fn first_word<'a, 'b>(s: &'a String, char: &'b String) -> &'a str {
    let bytes = s.as_bytes();
    let target_char = char.bytes().next().unwrap();
    for (i, &item) in bytes.iter().enumerate() {
        if item == target_char {
            return &s[0..i];
        }
    }
    &s[..]
}
